<?php

namespace app\api\subscribe;

use think\Event;
use think\facade\Db;
use think\facade\Queue;

class GroupOrder
{
    public function onGroupOrderTask($order_no)
    {
       $order =  Db::name('group_order')->field('id,user_id')->where('order_no',$order_no)->find();

       $group_activity_id = Db::name('group_activity_record')
                              ->where('group_order_id',$order['id'])
                              ->value('group_activity_id');

       $tasks = $this->getUserGroupOrderTask($order['user_id']);

       if (!$tasks->isEmpty()){
           foreach ($tasks as $v){
               if ($v['group_activity_id'] != 0 && $group_activity_id != $v['group_activity_id']){
                   continue;
               }
               $finish_join_num = $v['finish_join_num']+1;
               if ($v['join_num'] == $finish_join_num){
                   $this->changeUserTask($v['id'],['finish_join_num' => $finish_join_num,'task_status' => 2]);
                   Queue::push('app\store\job\Account@integral',[
                       'user_id' => $order['user_id'],
                       'desc' => '参与拼团',
                       'change_type'=>1,
                       'integral' => $v['give_integral']
                   ]);

                   if (!empty($v['give_coupon'])){
                       foreach ($v['give_coupon'] as $v1){
                           Queue::push('app\store\job\Coupon',[
                               'user_id' => $order['user_id'],
                               'coupon_id' => $v1['coupon_id'],
                               'num' => $v1['num'],
                           ],'userCoupon');
                       }
                   }
               }else {
                   $this->changeUserTask($v['id'],['finish_join_num' => $finish_join_num]);
               }
           }
       }

    }


    public function subscribe(Event $event)
    {
        $event->listen('GroupOrder',[$this,'onGroupOrderTask']);
    }

    private function getUserGroupOrderTask($user_id)
    {
        $tasks =  Db::name('user_task')
                    ->field('id,join_num,finish_join_num,group_activity_id,give_integral,give_coupon')
                    ->where(['task_type' => 3, 'user_id' => $user_id])
                    ->withAttr('give_coupon',function ($value){
                        if (empty($value)){
                            return [];
                        }
                        return json_decode($value,true);
                    })->whereBetweenTimeField('start_time','end_time')
                    ->select();

        return $tasks;
    }

    /**
     * 更改用户任务状态
     * */
    private function changeUserTask($user_task_id,$data)
    {
        Db::name('user_task')->where('id',$user_task_id)->update($data);
    }

    /**
     *
     * 发放积分
     * */
    private function changeUserIntegral($user_id,$give_integral)
    {
        Db::name('user')->where('user_id',$user_id)->inc('integral',$give_integral)->update();
    }
}